﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XcoAppSpaces.Contracts.Service;

namespace XcoAppSpaces.Transport.Sockets
{
	/// <summary>
	/// Defines extension methods for configuration of an XcoTCPTransportService using the fluent interface.
	/// </summary>
	public static class FluentConfigExtensions
	{
#if !SILVERLIGHT //no ports in silverlight
		/// <summary>
		/// Defines on which port the communication service should be running.
		/// </summary>
		/// <typeparam name="T">The type of the configuration object.</typeparam>
		/// <param name="config">The configuration object.</param>
		/// <param name="port">The port on which the communication service should be running.</param>
		/// <returns>The configuration object for further configuration with the fluent interface.</returns>
		public static T OnPort<T>(this IXcoServiceFluentConfiguration<XcoTCPTransportService, T> config, int port)
		{
			config.Service.Port = port;
			return config.FluentConfig;
		}

		/// <summary>
		/// Defines that the communication service should be running on a random port.
		/// </summary>
		/// <typeparam name="T">The type of the configuration object.</typeparam>
		/// <param name="config">The configuration object.</param>
		/// <returns>The configuration object for further configuration with the fluent interface.</returns>
		public static T OnRandomPort<T>(this IXcoServiceFluentConfiguration<XcoTCPTransportService, T> config)
		{
			config.Service.Port = 0;
			return config.FluentConfig;
		}
#endif

		/// <summary>
		/// Configures the connection lifetime (time after which a connection is closed when no messages are sent or received). 
		/// The default value is 1 minute. For some scenarios it can make sense to leave connections open for a longer time span 
		/// to reduce the need for sending messages just to keep the connection alive.
		/// </summary>
		/// <typeparam name="T">The type of the configuration object.</typeparam>
		/// <param name="config">The configuration object.</param>
		/// <param name="lifetime">The connection life time.</param>
		/// <returns>The configuration object for further configuration with the fluent interface.</returns>
		public static T WithConnectionLifetime<T>(this IXcoServiceFluentConfiguration<XcoTCPTransportService, T> config, TimeSpan lifetime)
		{
			config.Service.ConnectionLifetime = lifetime;
			return config.FluentConfig;
		}

		/// <summary>
		/// Defines a key that should be used for authentication of incoming connections.
		/// </summary>
		/// <typeparam name="T">The type of the configuration object.</typeparam>
		/// <param name="config">The configuration object.</param>
		/// <param name="authenticationKey">Key that should be used for authentication of incoming connections.</param>
		/// <returns>The configuration object for further configuration with the fluent interface.</returns>
		public static T WithAuthenticationKey<T>(this IXcoServiceFluentConfiguration<XcoTCPTransportService, T> config, string authenticationKey)
		{
			config.Service.SecuritySettings.AuthenticationKey = authenticationKey;
			return config.FluentConfig;
		}
	}
}
